//#因为遥控车，驱动没有速度输出，所以用CH1、CH2就行了，油门的CH3和左右横向位移的CH4没启用。
//#CH5、CH6是自定义开关，可以输出VRA、VRB（电位器），也可以输出SWA-SWD（SWC为3档，其他均为2档），这个留作以后选择小车模式。3档的SWC和2档的其他开关就有6种模式可以选择。。。。。

//#转向时使用和履带装置相同的转向方式，毕竟没装方向轮结构。



#define ch1  2
#define ch2  3
//#define ch3  4
//#define ch4  5
//#define ch5  6
//#define ch6  7
//#define chs  8

#define m1p1 9
#define m1p2 10
#define m2p1 11
#define m2p2 12


unsigned long INAIL;
unsigned long INELE;
//unsigned long INTHR;
//unsigned long INRUD;
//unsigned long INVRA;
//unsigned long INVRB;
//unsigned long INCHS;


int km = 0;

void setup()
{
  pinMode(m1p1, OUTPUT); //定义该接口为输出接口
  pinMode(m1p2, OUTPUT);
  pinMode(m2p1, OUTPUT);
  pinMode(m2p2, OUTPUT);

  pinMode(ch1, 0);
  pinMode(ch2, 0);
//  pinMode(ch3, 0);
//  pinMode(ch4, 0);
//  pinMode(ch5, 0);
//  pinMode(ch6, 0);
//  pinMode(chs, 0);
  Serial.begin(9600);
}
void loop()
{
  INAIL = int( pulseIn(ch1, 1) / 10);
  INELE = int( pulseIn(ch2, 1) / 10);
//  INTHR = pulseIn(ch3, 1);
//  INRUD = pulseIn(ch4, 1);
//  INVRA = pulseIn(ch5, 1);
//  INVRB = pulseIn(ch6, 1);
//  INCHS = pulseIn(chs, 1);

  ScanKEY();
  if (km == 1)
  {
    stay();
  }
}

void ScanKEY()
{
  km = 0;

  if (INAIL < 130)
  {
    L();
    if (INAIL < 130)
    {
      km = 1;
      while (INAIL > 130);
    }
  }
  if (INAIL > 160)
  {
    R();
    if (INAIL > 160)
    {
      km = 1;
      while (INAIL < 160);
    }
  }
  if (INELE < 130)
  {
    up();
    if (INELE < 130)
    {
      km = 1 ;
      while ( INELE  > 130 ) ;
    }
  }
  if (INELE > 160)
  {
    down();
    if (INELE > 160) {
      {
        km = 1 ;
        while (  INELE  < 160 ) ;
      }
    }
  }
}

void L()
{

  digitalWrite(m1p1, LOW); 
  digitalWrite(m1p2, HIGH);
  digitalWrite(m2p2, LOW); 
  digitalWrite(m2p1, HIGH);
  delay(500);
}

void R()
{
  digitalWrite(m1p2, LOW); 
  digitalWrite(m1p1, HIGH);
  digitalWrite(m2p1, LOW); 
  digitalWrite(m2p2, HIGH);
  delay(500);
}
void up()
{

  digitalWrite(m1p2, LOW); 
  digitalWrite(m1p1, HIGH);
  digitalWrite(m2p2, LOW); 
  digitalWrite(m2p1, HIGH);
  delay(500);
}

void down()
{
  digitalWrite(m1p1, LOW); 
  digitalWrite(m1p2, HIGH);
  digitalWrite(m2p1, LOW); 
  digitalWrite(m2p2, HIGH);
  delay(500);
}
void stay()
{
  digitalWrite(m1p1, LOW); 
  digitalWrite(m1p2, LOW);
  digitalWrite(m2p1, LOW); 
  digitalWrite(m2p2, LOW);
}
